# 电影之家
import html
import os
import shutil

import requests
import re

from util.headers import get_headers_init_user_agent

root = 'static/电影之家/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


url = 'https://www.jllfs.com'
list_response = requests.get(url)
html_content = html.unescape(list_response.text)
# print(html_content)
items = re.findall('<a href="https://jllfs.com/weihu/(.*?).html" title="(.*?)">',html_content,re.S)
for item in items:
    print(item)
    title = item[1]
    thumb_url = f'https://www.jllfs.com/weihu/{item[0]}.html'
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    thumb_content = html.unescape(thumb_response.text)
    contents = re.findall('<img class="yq63jQ lazyload" src="/upload/vod/(.*?).jpg" data-original="/upload/vod/(.*?).jpg" style="">',thumb_content,re.S)
    for content in contents:
        content_url = f'https://www.jllfs.com/upload/vod/{content[1]}.jpg'
        list_response = requests.get(content_url)
        with open(f"{root}/{title}_thumb.jpg", "wb") as f:
            f.write(list_response.content)

        break
    break

